﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.Model;
using MyOA.BLL;


namespace MyOA.UI.File
{
    public partial class RecycleBin : System.Web.UI.Page
    {
        RecycleBinManage re = new RecycleBinManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }
            if (!Page.IsPostBack)
            {
                DisplayDeletedFiles();
            }
        }

        protected void DisplayDeletedFiles()
        {
            IList<FileInfo> filelist = re.GetAllDeletedFiles();
            gvRecycleFile.DataSource = filelist;
            gvRecycleFile.DataBind();

        }

        protected void gvRecycleFile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string fileId = gvRecycleFile.DataKeys[e.Row.RowIndex].Value.ToString();//得到文件Id
                ImageButton imgbtnTrueDelete = (ImageButton)e.Row.FindControl("imgbtnTrueDelete");//实例化image按钮控件
                imgbtnTrueDelete.Attributes.Add("onclick", "return confirm('该操作是不可还原的，确定要删除吗？');");
                imgbtnTrueDelete.CommandArgument = fileId;//指定删除按钮的关联参数



                e.Row.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
                e.Row.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";
            }
        }

        protected void gvRecycleFile_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "resume")
            {
                int fileId = int.Parse(e.CommandArgument.ToString());//根据关联参数得到文件id


                bool result = re.ModifyFileInfoById(fileId);//修改文件
                if (result)
                {
                    DisplayDeletedFiles();//重新绑定文件信息
                }
                else
                {
                    Response.Write("还原过程中出现异常错误！");
                }
            }

            if (e.CommandName == "trueDelete")
            {
                int fileId = int.Parse(e.CommandArgument.ToString());//根据关联参数得到文件id

                bool result = re.DeleteFileInfoByFileId(fileId);
                if (result)
                {

                    DisplayDeletedFiles();//重新绑定文件信息
                }
                else
                {
                    Response.Write("永久删除时出现异常错误，请联系管理员！");
                }

            }
        }
    }

}
